package cn.shyouchi.wuguanjia.android.ui.event.checkpoint;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.widget.Button;

import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemDragListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class EventCheckPointActivity extends BaseActivity implements CheckPointContract.View {

    public final static int REQUEST_TYPE_ADD = 10001;
    public final static int REQUEST_TYPE_ADD_SUCCESS = 10002;


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_check_point)
    RecyclerView mRvCheckPoint;
    @BindView(R.id.btn_add_check_point)
    Button mBtnAddCheckPoint;

    CheckPointPresenter mCheckPointPresenter;
    String projectId;
    List<CheckPointEntity.ResultMessageBean> mPointEntities = new ArrayList<>();
    CheckPointAdapter mAdapter;

    private ItemTouchHelper mItemTouchHelper;
    private ItemDragAndSwipeCallback mItemDragAndSwipeCallback;


    boolean needRefresh = true;


    @Override
    public int initContentView() {
        return R.layout.activity_event_check_point;
    }

    @Override
    public void initInjector() {
        mCheckPointPresenter = new CheckPointPresenter(mContext, this);
        projectId = getIntent().getStringExtra("projectId");

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        EventBus.getDefault().register(this);
        mAdapter = new CheckPointAdapter(mPointEntities, this);
        mRvCheckPoint.setLayoutManager(new LinearLayoutManager(mContext));

        OnItemDragListener listener = new OnItemDragListener() {
            @Override
            public void onItemDragStart(RecyclerView.ViewHolder holder, int i) {
                LogUtil.e("Drag Start");
            }

            @Override
            public void onItemDragMoving(RecyclerView.ViewHolder holder, int i, RecyclerView.ViewHolder viewHolder1, int i1) {
                LogUtil.e("Drag Move");
            }

            @Override
            public void onItemDragEnd(RecyclerView.ViewHolder holder, int i) {
                LogUtil.e("Drag End");

                LogUtil.json(mAdapter.getData());
                mCheckPointPresenter.sortPoint(projectId, mAdapter.getData());
            }
        };
        mItemDragAndSwipeCallback = new ItemDragAndSwipeCallback(mAdapter);
        mItemTouchHelper = new ItemTouchHelper(mItemDragAndSwipeCallback);
        mItemTouchHelper.attachToRecyclerView(mRvCheckPoint);

        mAdapter.enableDragItem(mItemTouchHelper);
        mAdapter.setOnItemDragListener(listener);
        mRvCheckPoint.setAdapter(mAdapter);

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @OnClick(R.id.btn_add_check_point)
    public void onClick() {
        Intent intent = new Intent(mContext, AddEventCheckPointActivity.class);
        intent.putExtra("projectId", projectId);
        startActivityForResult(intent, REQUEST_TYPE_ADD);
    }

    @Override
    public void onDataRefresh(List<CheckPointEntity.ResultMessageBean> resultMessageBeanList) {
        mPointEntities.clear();
        mPointEntities.addAll(resultMessageBeanList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(CheckPointEntity.ResultMessageBean bean) {

        LogUtil.json(bean);

        Intent intent = new Intent(mContext, PreviewCheckPointActivity.class);
        intent.putExtra("checkpoint", bean);
        intent.putExtra("projectId", projectId);
        startActivity(intent);
    }

    @Override
    public void onDelClick(CheckPointEntity.ResultMessageBean bean) {
        mCheckPointPresenter.delPoint(projectId, String.valueOf(bean.getId()));
    }

    @Override
    public void onEditClick(CheckPointEntity.ResultMessageBean bean) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_TYPE_ADD && resultCode == REQUEST_TYPE_ADD_SUCCESS) {
            mCheckPointPresenter.getPointList(projectId);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (needRefresh) {
            mCheckPointPresenter.getPointList(projectId);
            needRefresh = false;
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DataChange(DataChangeEvent event) {
        needRefresh = true;
    }

}
